/**
 * Created by smp on 2017/3/17.
 */
var isSearchBarExpand = false;
var index;

var modelPath=ROOT_PATH+'/permission/roleGroup/';
function doSearch(value, name) {
    alert('You input: ' + value + '(' + name + ')');
}
function toggleSearch() {
    var $mainLayout = $('#main_layout');
    var expandHeight = 200;
    var collapseHeight = 50;
    if (!isSearchBarExpand){
        $mainLayout.layout('panel','north').panel('resize',{height:expandHeight});
        $mainLayout.layout('resize');
        isSearchBarExpand = true;
    }
    else {
        $mainLayout.layout('panel','north').panel('resize',{height:collapseHeight});
        $mainLayout.layout('resize');
        isSearchBarExpand = false;
    }
}
/**
 * 关闭layer
 */
function closeLayer() {
    layer.close(index);
}
/**
 * 使用layer 打开一个页面
 * @param title
 * @param content
 */
function openLayer(title,content) {

    index=layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        area: ['600px', '380px'],
        content: modelPath+content
    });
}
function result(data) {
    if(data==1){
        layer.msg("保存成功");
        closeLayer();
    }else{
        layer.alert("保存失败");
    }
}
$(function () {
    $('#roleGroupList').datagrid({
        method:'post',
        iconCls: 'icon-edit',//图标
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect:true,
        fitColumns: true,
        pagination: true,
        url: ROOT_PATH + '/permission/roleGroup/list.shtml',
        queryParams: {
            sort: 'sort',
            order: 'asc'
        },
        columns:[[
            {field: 'id',checkbox: true},
            {field:'code',title:'角色组标识',width:100,sortable:true},
            {field:'name',title:'角色组名称',width:100,sortable:true},
            {field:'sort',title:'排序号',width:100,sortable:true,hidden: true},
            {field:'remarks',title:'备注',width:100,sortable:true}
        ]],
        toolbar: '#toolbar'
    });
    /**
     * 查询按钮
     */
    $('#selectRoleGroup').on('click',function(){
        var name=$('#name').val();

        $('#roleGroupList').datagrid('load', {name:name,sort: 'sort', order: 'asc'});
    });

    //添加按钮事件
    $('#btn_roleGroup_add').on('click',function () {

        var title='添加角色组';
        var content='addRoleGroup.shtml';
        openLayer(title,content);
    });
    //编辑按钮事件
    $('#btn_roleGroup_edit').on('click',function () {

        var checkedRows=$('#roleGroupList').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要编辑的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var id=checkedRows[0].id;
        var title='编辑角色组';
        var content='editRoleGroup.shtml?id='+id;
        openLayer(title,content);
    });
    //删除按钮事件
    $('#btn_roleGroup_delete').on('click',function () {

        var checkedRows=$('#roleGroupList').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要删除的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var id=checkedRows[0].id;
        layer.confirm('是否确定删除当前选中数据？', {
            btn: ['确定','取消'] //按钮
        }, function(){
        $.ajax({
            type: "POST",
            url: ROOT_PATH+'/permission/role/deleteRoleGroupJudgement.shtml',
            data: {groupId:id},
            dataType: "json",
            success: function(result){
                if (result.success) {
                        $.ajax({
                            type: "POST",
                            url: ROOT_PATH+'/permission/roleGroup/deleteRoleGroup.shtml',
                            data: {id:id,delState:1},
                            dataType: "json",
                            success: function(result){
                                if (result.success) {
                                    $('#roleGroupList').datagrid('clearSelections').datagrid('reload');
                                    layer.msg('删除成功', {
                                        icon: 1,
                                        time: 1000
                                    });
                                }
                                else {
                                    layer.msg('删除失败', {
                                        icon: 2,
                                        time: 2000
                                    });
                                }
                            }
                        });
                }
                else {
                    layer.msg('请先删除该角色组下的角色', {
                        icon: 2,
                        time: 2000
                    });
                }
            }
        });
        });
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
    });
});